<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Form jQuery Validation</title>
    <link rel="shortcut icon" type="image/x-icon" href="Vertical/favicon.ico">
    <!-- google font -->
    <link rel="shortcut icon" href="favicon.ico">
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <%--    <link href="${pageContext.request.contextPath}/staitc/css/animate.css" rel="stylesheet">--%>
    <link href="${pageContext.request.contextPath}/static/css/style.css?v=4.1.0" rel="stylesheet">

    <!--form semantic style-->
    <%--    <link href="${pageContext.request.contextPath}/static/style.css" rel="stylesheet">--%>
    <%--    <link href="${pageContext.request.contextPath}/static/" rel="stylesheet">--%>
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h3>院系信息</h3>
                </div>
                <div class="ibox-content">
                    <div style="float: left;margin-left: 4%">
                        <a onclick=" " href="" class="btn btn-primary "><i class="fa fa-refresh"></i></a>
                    </div>
                    <div style="margin-left: 85%">
                        <input type="text" style="width: 200px;height: 30px;" id="studentName"
                               placeholder="按学生名字查询，输入关键字即可">
                        <button class="btn btn-primary" onclick="selectStudentByName()">搜索</button>
                    </div>
                </div>
                <table class="table table-striped table-bordered table-hover " id=""
                       style="text-align: center;width: 90%;margin-left: 5%;margin-right: 5%">
                    <thead>
                    <tr>
                        <%--                <td>#</td>--%>
                        <td>学生学号</td>
                        <td>学生姓名</td>
                        <td>学生年龄</td>
                        <td>学生电话</td>
                        <td>学生性别</td>
                        <td>所在班级</td>
                        <td>所在院系</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody id="tb">
                    </tbody>

                </table>
                <!-- 分页区域-->
                <div class="margin" style="margin-left: 5%">
                    每页显示<select id="pageSizeSelect" onchange="onSelectChange()">
                    <option value="5" selected="selected">5</option>
                    <option value="10">10</option>
                    <option value="15">15</option>
                </select>条数据，当前是第<span id="currentPageSpan"></span>页，
                    总共<span id="totalPageSpan"></span> 页
                    <button type="button" class="btn btn-success" onclick="firstPage()">首页</button>
                    <button type="button" class="btn btn-success" onclick="prePage()">上一页</button>
                    <button type="button" class="btn btn-success" onclick="nextPage()">下一页</button>
                    <button type="button" class="btn btn-success" onclick="lastPage()">尾页</button>
                </div>
            </div>
        </div>
    </div>
    <%--    这里的div做一个编辑用户信息的模态框--%>
    <div class="modal" id="editModal">
        <%--对话框--%>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="ibox float-e-margins">
                    <div class="modal-header">
                        <h4>学生信息</h4>
                    </div>
                    <div class="modal-body">
                        <label class="control-label">学生学号</label>
                        <input class="form-control" id="sId" placeholder="请输入学生姓名">
                        <label class="control-label">学生姓名</label>
                        <input class="form-control" id="sName" placeholder="请输入学生姓名">
                        <label class="control-label">学生年龄</label>
                        <input class="form-control" id="sAge" placeholder="请输入学生年龄">
                        <label class="control-label">电话</label>
                        <input class="form-control" id="sPhone" placeholder="请输入学生电话">
                        <label class="control-label">性别 </label>
                        <select id="sSex" class=form-control>
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                        <label class="control-label">班级</label>
                        <input class="form-control" id="sClass" placeholder="请输入班级id">
                    </div>
                    <div class="modal-footer">
                        <button class="button" data-dismiss="modal">取消</button>
                        <button class="button" onclick="updateok()">确定</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 全局js -->
<script src="${pageContext.request.contextPath}/static/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/layer/layer.min.js"></script>

<!-- 自定义js -->
<script src="${pageContext.request.contextPath}/static/js/hplus.js?v=4.1.0"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/contabs.js"></script>
<!-- 第三方插件 -->
<script src="${pageContext.request.contextPath}/static/js/plugins/pace/pace.min.js"></script>
<script>
    //当前页码
    var currentPage = 1;
    //每页的记录个数
    var pageSize = 5;
    //总共多少页
    var totalPage;
    $(function () {
        //显示当前分页数据
        loadListByPage();

    });

    //用户修改的每页的记录数
    function onSelectChange() {
        pageSize = $("#pageSizeSelect").val();
        loadListByPage();
    }

    //首页的回调方法
    function firstPage() {
        currentPage = 1;
        loadListByPage();
    }

    //上一页回调方法
    function prePage() {
        currentPage--;
        if (currentPage < 1) {
            currentPage = 1;
        }
        loadListByPage();
    }

    //下一页回调方法
    function nextPage() {
        currentPage++;
        if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        loadListByPage();
    }

    //尾页回调方法
    function lastPage() {
        currentPage = totalPage;
        loadListByPage();
    }

    function showList(departmentList) {
        var html = '';
        //遍历userList数组
        for (var i = 0; i < departmentList.length; i++) {
            //拿到一个用户信息
            var item = departmentList[i];
            html += "<tr>";
            html += "<td>" + item.studentId + "</td>"; //显示学生ID
            html += "<td>" + item.studentName + "</td>"; //显示学生姓名
            html += "<td>" + item.studentAge + "</td>";//显示缺勤学生的名字
            html += "<td>" + item.studentPhone + "</td>";//显示学生电话
            if (item.studentSex == 1) {
                html += "<td>男</td>";
            } else {
                html += "<td>女</td>";
            }
            html += "<td>" + item.className + "</td>";//显示学生班级
            html += "<td>" + item.departmentName + "</td>";//显示学生班级
            html += "<td><button type=\"button\" class=\"btn btn-success\" onclick='updatedp(\"" + item.studentId + "\")'>更改院系</button>"
            html += "</tr>";
        }
        $("#tb").html(html);
    }

    //分页显示用户信息
    function loadListByPage() {
        $.ajax({
            url: "${pageContext.request.contextPath}/departmentController/departmentByPage",
            type: 'get',
            data: {"currentPage": currentPage, "pageSize": pageSize},
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    showList(result.departmentList);
                    totalPage = result.totalPage;
                    $("#totalPageSpan").html(totalPage);
                    $("#currentPageSpan").html(currentPage);
                }
            },
            error: function (err) {
                alert("ajax请求失败");
            }
        });
    }

    //根据学号查询学生信息然后在模态框上显示
    function updatedp(studentId) {
        $.ajax({
            url: "${pageContext.request.contextPath}/departmentController/getone",
            type: 'get',
            data: {"studentId": studentId},
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            success: function (result) {
                if (result.code == 0) {
                    showEdiModal(result.student);
                } else {
                    alert(result.message);
                }
            },
            error: function (err) {
                console.log("ajax请求失败");
            }
        });
    }

    //显示模态框的信息
    function showEdiModal(student) {
        $("#sId").val(student.studentId);//学生ID
        $("#sName").val(student.studentName);//学生姓名
        $("#sAge").val(student.studentAge);//年龄
        $("#sPhone").val(student.studentPhone);//电话
        $("#sSex").val(student.studentSex);//性别
        $("#sClass").val(student.className);//班级
        //除了班级，其他的都不可以修改
        $("#sId").attr("disabled", "disabled");
        $("#sName").attr("disabled", "disabled");
        $("#sAge").attr("disabled", "disabled");
        $("#sPhone").attr("disabled", "disabled");
        $("#sPhone").attr("disabled", "disabled");
        $("#sSex").attr("disabled", "disabled");
        //设置模态框的开启状态
        $("#editModal").modal("show");
    }

    function updateok() {
        var studentId = $("#sId").val();
        var className = $("#sClass").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/departmentController/updateok",
            type: "post",
            contentType: 'application/x-www-form-urlencoded',
            data: {studentId: studentId, className: className},
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    $("#editModal").modal("hide");
                    loadListByPage();
                }
            },
            error: function (error) {
                alert("更改院系ajax请求失败")
            }
        })

    }

    //模糊查询
    //根据姓名查询学生
    function selectStudentByName() {
        var studentName = $("#studentName").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/departmentController/seletedByName",
            type: "get",
            data: {"studentName": studentName},
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            success: function (result) {
                if (result.code == 0) {
                    showList(result.student);
                } else {
                    alert(result.message);
                }
            },
            error: function (err) {
                alert("模糊查询失败ajax请求失败");
            }

        })
    }
</script>
</body>
</html>
